//
//  XMMaskPaintView.m
//  XiaoMiao
//
//  Created by 23 on 2018/2/24.
//  Copyright © 2018年 smallyou. All rights reserved.
/*
 马赛克绘制视图
 */

#import "XMMaskPaintView.h"

@interface XMMaskPaintView ()

@property (nonatomic, strong) UIImageView *surfaceImageView;

@property (nonatomic, strong) CALayer *imageLayer;

@property (nonatomic, strong) CAShapeLayer *shapeLayer;


/**临时路径*/
@property (nonatomic, assign) CGMutablePathRef tmpPath;
/**橡皮擦的路径*/
@property (nonatomic, assign) CGMutablePathRef earserPath;

//马赛克路径
@property (nonatomic,strong) NSMutableArray * shapeLayerPaths;
//橡皮擦路径
@property (nonatomic,strong) NSMutableArray * earserPaths;

@end


@implementation XMMaskPaintView

- (void)dealloc
{
    if (self.path) {
        CGPathRelease(self.path);
    }
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        //添加imageview（surfaceImageView）到self上
        self.surfaceImageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:self.surfaceImageView];
        
        //添加layer（imageLayer）到self上
        self.imageLayer = [CALayer layer];
        self.imageLayer.frame = self.bounds;
        [self.layer addSublayer:self.imageLayer];
        
        self.shapeLayer = [CAShapeLayer layer];
        self.shapeLayer.frame = self.bounds;
        self.shapeLayer.lineCap = kCALineCapRound;
        self.shapeLayer.lineJoin = kCALineJoinRound;
        self.shapeLayer.lineWidth = 10.f;
        self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
        self.shapeLayer.fillColor = nil;//此处设置颜色有异常效果，可以自己试试
        
        [self.layer addSublayer:self.shapeLayer];
        self.imageLayer.mask = self.shapeLayer;


    }
    return self;
}

- (void)setImage:(UIImage *)image
{
    //底图 - 马赛克图片
    _image = image;
    self.imageLayer.contents = (id)image.CGImage;
}

- (void)setSurfaceImage:(UIImage *)surfaceImage
{
    //顶图 - 原图
    _surfaceImage = surfaceImage;
    self.surfaceImageView.image = surfaceImage;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    if (self.path == nil) {
        self.path = CGPathCreateMutable();
    }
    
    if (self.type == XMMaskPaintViewTypeMask) {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        CGPathMoveToPoint(self.path, NULL, point.x, point.y);
        CGMutablePathRef path = CGPathCreateMutableCopy(self.path);
        self.shapeLayer.path = path;
        CGPathRelease(path);
    }else{
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        if(CGPathContainsPoint(self.path, nil, point, YES))
        {
            //橡皮擦的点在路径里面
            //清除路径
            [self backPath];
        }
    }
    
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    
    if (self.type == XMMaskPaintViewTypeMask) {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        CGPathAddLineToPoint(self.path, NULL, point.x, point.y);
        CGMutablePathRef path = CGPathCreateMutableCopy(self.path);
        self.shapeLayer.path = path;
        CGPathRelease(path);
    }else{
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        if(CGPathContainsPoint(self.path, nil, point, YES))
        {
            //橡皮擦的点在路径里面
            //清除路径
            [self backPath];
        }
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    
   
}

//撤销
- (void)backPath
{
    self.tmpPath = self.path;
    self.path = CGPathCreateMutable();
    self.shapeLayer.path = nil;
    
}

//前进
- (void)forwardPath
{
    CGMutablePathRef path = CGPathCreateMutableCopy(self.tmpPath);
    self.shapeLayer.path = path;
    self.path = path;
}


/**判断两个路径是否相交*/
- (BOOL)jadgePath:(CGPathRef)path1 path:(CGPathRef)path2
{
    
    return YES;
}




@end
